set more off
clear
cd "D:\Projects\HeterogeneousExternalities\Data\"

use Daily_Data.dta,clear

*Remove counties with mith multiple regulations and set up an event time window
drop if multiple>1
sort station_fe date

gen RVP9_ramp_on=0
gen RVP78_ramp_on=0
gen RVP7_ramp_on=0
gen RFG_ramp_on=0

gen RVP9_ramp_off=0
gen RVP78_ramp_off=0
gen RVP7_ramp_off=0
gen RFG_ramp_off=0

local ramp_length=30
sort station_fe date
forvalues i=1/`ramp_length'{
bysort station_fe: replace RVP9_ramp_on=0-`i'*(1/`ramp_length') if RVP9_on[_n+`i']==1
bysort station_fe: replace RVP9_ramp_on=1-`i'*(1/`ramp_length') if RVP9_on[_n-`i'+1]==1

bysort station_fe: replace RVP9_ramp_off=1-`i'*(1/`ramp_length') if RVP9_off[_n-`i'+1]==1
bysort station_fe: replace RVP9_ramp_off=0-`i'*(1/`ramp_length') if RVP9_off[_n+`i']==1


bysort station_fe: replace RVP78_ramp_on=0-`i'*(1/`ramp_length') if RVP78_on[_n+`i']==1
bysort station_fe: replace RVP78_ramp_on=1-`i'*(1/`ramp_length') if RVP78_on[_n-`i'+1]==1

bysort station_fe: replace RVP78_ramp_off=1-`i'*(1/`ramp_length') if RVP78_off[_n-`i'+1]==1
bysort station_fe: replace RVP78_ramp_off=0-`i'*(1/`ramp_length') if RVP78_off[_n+`i']==1


bysort station_fe: replace RVP7_ramp_on=0-`i'*(1/`ramp_length') if RVP7_on[_n+`i']==1
bysort station_fe: replace RVP7_ramp_on=1-`i'*(1/`ramp_length') if RVP7_on[_n-`i'+1]==1

bysort station_fe: replace RVP7_ramp_off=1-`i'*(1/`ramp_length') if RVP7_off[_n-`i'+1]==1
bysort station_fe: replace RVP7_ramp_off=0-`i'*(1/`ramp_length') if RVP7_off[_n+`i']==1

bysort station_fe: replace RFG_ramp_on=0-`i'*(1/`ramp_length') if RFG_on[_n+`i']==1
bysort station_fe: replace RFG_ramp_on=1-`i'*(1/`ramp_length') if RFG_on[_n-`i'+1]==1

bysort station_fe: replace RFG_ramp_off=1-`i'*(1/`ramp_length') if RFG_off[_n-`i'+1]==1
bysort station_fe: replace RFG_ramp_off=0-`i'*(1/`ramp_length') if RFG_off[_n+`i']==1

}

*Generate residuals for vehicle counts to control for what sensors are present on a given day
local fe="station_fe date"
reghdfe veh, absorb(`fe') resid
predict y,resid

gen reg_on=RVP9_ramp_on+RVP78_ramp_on+RVP7_ramp_on+RFG_ramp_on
drop if reg_on<-1
drop if reg_on>1

gen reg_off=RVP9_ramp_off+RVP78_ramp_off+RVP7_ramp_off+RFG_ramp_off
drop if reg_off<-1
drop if reg_off>1


save tempfull.dta,replace
preserve
collapse (mean) rgas veh y, by(reg_on)
sort  reg_on
save tempon.dta,replace
restore

preserve
collapse (mean) rgas veh y, by(reg_off)
sort  reg_off
save tempoff.dta,replace
restore


use tempon.dta, clear
sort reg_on

twoway lfit rgas reg_on if reg_on<0, lstyle(p7) lpattern(l) || lfit rgas reg_on if reg_on>0 ,lstyle(p7) lpattern(l)|| scatter rgas reg_on if reg_on!=0 , legend(off) xli(0) msymbol(O) mcolor(gs5) mcolor(gs5%25) msize(medlarge)/*
	*/ ytitle(Gasoline Price ($'s)) xtitle(Event Time) xlabel(-1 "-30 Days" 0 "Regulation On" 1 "+30 Days") /*
	*/  name(gason,replace)   ylabel(,nogrid) xlabel(,nogrid) ylabel(2.8(.2)3.4) nodraw
	
twoway lfit y reg_on if reg_on<0, lstyle(p7) lpattern(l) || lfit y reg_on if reg_on>0 ,lstyle(p7) lpattern(l)|| scatter y reg_on if reg_on!=0 , legend(off) xli(0) msymbol(O) mcolor(gs5) mcolor(gs5%50) msize(medlarge)/*
	*/ ytitle(Traffic Count Residuals) xtitle(Event Time) xlabel(-1 "-30 Days" 0 "Regulation On" 1 "+30 Days") /*
	*/  name(vehon,replace)   ylabel(,nogrid) xlabel(,nogrid)  ylabel(-3(2)3) nodraw


use tempoff.dta,clear
sort reg_off

twoway lfit rgas reg_off if reg_off<0,lstyle(p7) lpattern(l) || lfit rgas reg_off if reg_off>0,lstyle(p7) lpattern(l) || scatter rgas reg_off if reg_off!=0 , legend(off) xlin(0) msymbol(O) mcolor(gs5) mcolor(gs5%25) msize(medlarge)/*
*/  ytitle(Gasoline Price ($'s)) xtitle(Event Time) xlabel(-1 "-30 Days" 0 "Regulation Off" 1 "+30 Days") /*
*/ name(gasoff,replace)   ylabel(,nogrid) xlabel(,nogrid) ylabel(2.8(.2)3.4) nodraw

twoway lfit y reg_off if reg_off<0,lstyle(p7) lpattern(l) || lfit y reg_off if reg_off>0,lstyle(p7) lpattern(l) || scatter y reg_off if reg_off!=0 , legend(off) xlin(0) msymbol(O) mcolor(gs5) mcolor(gs5%50) msize(medlarge)/*
*/  ytitle(Traffic Count Residuals) xtitle(Event Time) xlabel(-1 "-30 Days" 0 "Regulation Off" 1 "+30 Days") /*
*/ name(vehoff,replace)  ylabel(,nogrid) xlabel(,nogrid) ylabel(-3(2)3) nodraw 

	graph combine gason gasoff , col(2) graphregion(color(white) margin(tiny)) title(Gasoline Prices) name(Apanel,replace) nodraw
	graph combine vehon vehoff, col(2) graphregion(color(white) margin(tiny)) title(Traffic Counts) name(Bpanel,replace) nodraw

	graph combine Apanel Bpanel, col(1) graphregion(color(white) margin(none)) iscale(1) name(Full,replace)

	
erase tempfull.dta
erase tempon.dta
erase tempoff.dta
